﻿using UnityEngine;
using System.Collections;
using System.IO;

public class Board : MonoBehaviour {

	private Level leveldata;

	public Transform wallUp;
	public Transform wallRight;
	public Transform pawn;
	
	void Start () {
		leveldata = Level.ReadLevel("lvl1");

		foreach (Node n in leveldata.nodes)
		{
			Debug.Log(n.posX + " " + n.posY);

			if(n.blockUp)
			{
				Transform wallInstance = Instantiate(wallUp) as Transform;
				float x = n.posX * GameConstants.mapUnit - GameConstants.mapUnit * 0.5f;
				float y = n.posY * GameConstants.mapUnit - GameConstants.mapUnit * 0.5f;
				wallInstance.transform.position = new Vector3(x, 0.5f, y);
			}
			if(n.blockRight)
			{
				Transform wallInstance = Instantiate(wallRight) as Transform;
				float x = n.posX * GameConstants.mapUnit - GameConstants.mapUnit * 0.5f;
				float y = n.posY * GameConstants.mapUnit - GameConstants.mapUnit * 0.5f;
				wallInstance.transform.position = new Vector3(x, 0.5f, y);
			}
			if(n.pawnColor != Node.PawnColor.none)
			{
				Transform pawnInstance = Instantiate(pawn) as Transform;
				float x = n.posX * GameConstants.mapUnit - GameConstants.mapUnit * 0.5f;
				float y = n.posY * GameConstants.mapUnit - GameConstants.mapUnit * 0.5f;
				pawnInstance.transform.position = new Vector3(x, 0.5f, y);

				Material mat;
				MeshRenderer mesh;
				switch(n.pawnColor) {
					case Node.PawnColor.yellow:
						mat = Resources.Load("Materials/tower2") as Material;
						mesh = pawnInstance.GetComponent<MeshRenderer>();
						mesh.material = mat;
						break;
					case Node.PawnColor.red:
						mat = Resources.Load("Materials/tower") as Material;
						mesh = pawnInstance.GetComponent<MeshRenderer>();
						mesh.material = mat;
						break;
				}

			
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
